
$(function() {
    validateRule();
	$('.imgcode').click(function() {
		var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
		$(".imgcode").attr("src", url);
	});
});

$.validator.setDefaults({
    submitHandler: function() {
        needCode();
    }
});
function needCode() {
    var username = $.common.trim($("input[name='username']").val());
    $.ajax({
        type: "post",
        url: ctx + "needCode",
        data: {
            "username": username
        },
        success: function(r) {
            if (r.code == 0 && r.data==='noNeed') {
                login();
            } else if (r.code == 0 && r.data!=='noNeed') {
                layer.open({
                    //title: '请输入手机号为'+r.data+'的验证码'
                    title: '请输入验证码'
                    ,content: '<div style="color: black;"><label>请输入验证码:&nbsp;&nbsp;</label><input id="phoneCode" type="text" placeholder="请输入验证码" style="width: 200px"></div>\n',
                    btn: ['确定', '取消'],
                    btn1: function(index, layero){
                        var val = $('#phoneCode').val();
                        if(val.trim()===''){
                            alert('请输入验证码');
                            return false;
                        }
                        login(val);
                    },
                    btn2: function(index, layero){
                    }
                });
            }else {
                $.modal.closeLoading();
                $('.imgcode').click();
                $(".code").val("");
                $.modal.msg(r.msg);
            }
        }
    });
}

function login(code) {
	$.modal.loading($("#btnSubmit").data("loading"));
	var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": md5(password),
            "validateCode" : validateCode,
            "rememberMe": rememberMe,
            "phoneCode": code
        },
        success: function(r) {
            if (r.code == 0) {
                location.href = ctx + 'index';
            } else {
                $.modal.closeLoading();
                $('.imgcode').click();
                $(".code").val("");
                $.modal.msg(r.msg);
            }
        }
    });
}


function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
            },
            password: {
                required: icon + "请输入您的密码",
            }
        }
    })
}
